package com.zhouchao.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.naming.ldap.Rdn;

import com.zhouchao.bean.DormBuild;
import com.zhouchao.bean.RepairBean;
/**
 * 报修管理
 * 
 * @author zhouchao 2015-4-10
 *
 */
public class RepairDao {
	/**
	 * 列出所有的报修信息
	 * @param con
	 * @return
	 * @throws Exception
	 */
	
	public List<RepairBean> repairList(Connection con)throws Exception {
		List<RepairBean> repairList = new ArrayList<RepairBean>();
		String sql = "select * from t_repair";
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while(rs.next()) {
			RepairBean repair=new RepairBean();
			repair.setId(rs.getInt("id"));
			repair.setDate(rs.getString("date"));
			repair.setName(rs.getString("name"));
			repair.setTel(rs.getString("tel"));
			repair.setContent(rs.getString("content"));
			repair.setEnd(rs.getInt("end"));
			repair.setBuildid(rs.getInt("buildid"));
			repair.setRoom(rs.getString("room"));
			repairList.add(repair);
		}
		return repairList;
	}
	
	public List<RepairBean> repairList(Connection con,String buildid,String room)throws Exception {
		List<RepairBean> repairList = new ArrayList<RepairBean>();
		String sql = "select * from t_repair where buildid='"+buildid+"' and room='"+room+"'";
		System.out.println(sql);
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while(rs.next()) {
			RepairBean repair=new RepairBean();
			repair.setId(rs.getInt("id"));
			repair.setDate(rs.getString("date"));
			repair.setName(rs.getString("name"));
			repair.setTel(rs.getString("tel"));
			repair.setContent(rs.getString("content"));
			repair.setEnd(rs.getInt("end"));
			repair.setBuildid(rs.getInt("buildid"));
			repair.setRoom(rs.getString("room"));
			repairList.add(repair);
		}
		return repairList;
	}
	
	
	public int repairAdd(Connection con, RepairBean repair)throws Exception {
		String sql = "insert into t_repair values(null,?,?,?,?,?,?,?)";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, repair.getDate());
		pstmt.setString(2, repair.getName());
		pstmt.setString(3, repair.getTel());
		pstmt.setString(4, repair.getContent());
		pstmt.setInt(5, repair.getEnd());
		pstmt.setInt(6, repair.getBuildid());
		pstmt.setString(7, repair.getRoom());
		return pstmt.executeUpdate();
	}
	
	public int repairDelete(Connection con, String repairId)throws Exception {
		String sql = "delete from t_repair where repairId=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, repairId);
		return pstmt.executeUpdate();
	}
	
	public int repairUpdate(Connection con, String repairId)throws Exception {
		String sql = "update t_repair set end=? where id=?";
		
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setInt(1, 1);
		pstmt.setString(2, repairId);
		System.out.println(sql);
		return pstmt.executeUpdate();
	}
}
